attribute vec4 a_Position;
attribute vec4 a_Color;
attribute vec4 a_Normal; // Normal
attribute vec2 a_texcoord;

uniform mat4 u_MvpMatrix;

uniform vec3 u_LightPosition; // Position of the light source (in the world coordinate system)----------------------------
uniform vec3 u_LightColor; // Light color----------------------------

uniform vec3 u_DiffuseLight; // Diffuse light color
uniform vec3 u_LightDirection; // Diffuse light direction (in the world coordinate, normalized)
uniform vec3 u_AmbientLight; // Color of an ambient light

varying vec4 v_Color;

varying vec2 v_texcoord;

void main() {
    gl_Position = u_MvpMatrix * a_Position;
    // Make the length of the normal 1.0
    vec3 normal = normalize(a_Normal.xyz);
    // The dot product of the light direction and the normal (the orientation of a surface)
    float nDotL = max(dot(u_LightDirection, normal), 0.0);

    // Calculate the light direction and make it 1.0 in length
    vec3 pointLightDirection = normalize(u_LightPosition - vec3(a_Position));
    // The dot product of the light direction and the normal
    float nPointLightDotL = max(dot(pointLightDirection, normal), 0.0);

    vec3 pointLightDiffuse = u_LightColor * a_Color.rgb * nPointLightDotL;
    
    // Calculate the color due to diffuse reflection
    vec3 diffuse = u_DiffuseLight * a_Color.rgb * nDotL;
    
    // Calculate the color due to ambient reflection
    vec3 ambient = u_AmbientLight * a_Color.rgb;
    // Add the surface colors due to diffuse reflection and ambient reflection
    v_Color = vec4(diffuse + ambient + pointLightDiffuse, a_Color.a);
    v_texcoord = a_texcoord;
}